{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca128868-6c5b-4e43-a7a7-75d47b4c5111",
   "metadata": {},
   "source": [
    "# 第一节、数组的创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19fdef07-c8a3-43cc-98a4-757c98a7ea00",
   "metadata": {},
   "source": [
    "创建数组的最简单方法就是使用array函数，将Python的list转换为ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9a69b1d3-846b-4249-adee-8600f8721033",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca38474e-28d8-45a7-ad45-40b21eb63e56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = list(range(5))\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "281a2794-72c4-45eb-b3e6-caf49112f95a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array(lst)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e7c7520-d880-43c9-829e-429c6d9f4da8",
   "metadata": {},
   "source": [
    "我们可以利用np中的一些内置函数来创建数组，比如我们创建全是0的数组，也可以创建全1数组，全是其他数字的数组，或者等差数列数组，正态分布数组，随机数等等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "885d95fa-4416-47f7-9631-16fec4aa8fce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np  # 为什么我要反复导入？只是重复多联系几遍\n",
    "\n",
    "# 创建全是1的单位矩阵\n",
    "arr1 = np.ones(10)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "503bd643-229d-474c-ab74-b745afd101ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一个参数是shape如果只传入一个数组，那么就是一维的也可以写成\n",
    "arr2 = np.ones(shape=(10,))\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "501cdfb7-3078-45d6-95d6-7366422234dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全是0的ndarray\n",
    "arr3 = np.zeros(shape=(10,))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "90b1bc7e-08c5-410b-9b42-4917095a469f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全是你指定数字的ndarray\n",
    "arr4 = np.full(shape=(10,), fill_value=5)  # 参数fill_value就是你指定要填入的数\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a24ee603-1e66-476e-ad5e-11337df0e40c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照范围输出ndarray数组\n",
    "arr5 = np.arange(0, 10, 2)  # 三个参数依次是start,stop,step\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "76628047-b40c-4d1f-993f-26b7644a6ad4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成一个等差数列ndarray\n",
    "arr6 = np.linspace(1, 10, num=10)  # 参数依次是start,stop,num是生成多少个,函数会自动计算公差d\n",
    "arr6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ba793650-41f4-4426-9607-64e5443e39b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   2.,    4.,    8.,   16.,   32.,   64.,  128.,  256.,  512.,\n",
       "       1024.])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成等比数列ndarry\n",
    "arr7 = np.logspace(1, 10, num=10, base=2)\n",
    "# 参数依次是start,stop,num是生成多少个数,base是公比q,公比默认为10\n",
    "arr7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b7e40063-67b5-4187-9599-9aa283813774",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 16, 12, 10, 13, 13, 19, 16, 13, 10])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成整数随机数ndarray\n",
    "arr8 = np.random.randint(10, 20, size=(10,))\n",
    "# 参数是左开右闭的[start, stop).size是指定生成ndarray的形状\n",
    "arr8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5550dfe7-d037-40a3-926b-eaae1455a433",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.36268284, -0.79775267, -0.91876967],\n",
       "       [-1.19518637, -1.7999417 ,  1.66852453]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成标准正态分布的随机数：均值为0，标准差为1\n",
    "arr9 = np.random.randn(2, 3)  # 参数是维度  2,3 ==> 表示2行3列\n",
    "arr9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "364be3c1-f7b4-47f6-b22b-35d656b02eba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.43650193, 0.46609236, 0.40929928],\n",
       "       [0.46803839, 0.27448603, 0.7899081 ]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回一个[0.0, 1.0)之间的随机数ndarray\n",
    "arr10 = np.random.random(size=(2, 3))\n",
    "arr10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d546d0b-8605-4c66-a48b-5732e398580d",
   "metadata": {},
   "source": [
    "此外还有很多创建数组的方法，后续放在exercises练习题中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b101cf73-a940-4e77-a931-7d976140cef8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
